// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';
import moment from 'moment';

/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(options?: { [key: string]: any }) {
  return request<{
    data: API.CurrentUser;
  }>('/api/currentUser', {
    method: 'GET',
    ...(options || {}),
  });
}

export async function getLocalUser() {
  return request<{
    data: API.CurrentUser;
  }>('https://localhost:44301/connect/userinfo', {
    method: 'GET',
    ...({}),
  });
};

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/login/outLogin', {
    method: 'POST',
    ...(options || {}),
  });
}

export async function getAllIssue() {
  return request<API.LoginResult>('https://localhost:7047/api/app/issue', {
    method: 'GET'
  });
}

export async function refreshToken() {
  const expires_in = localStorage.getItem('expires_in');
  if (!expires_in) {
    return;
  }
  const timestamp = moment().unix();
  const expiresTimestamp = parseInt((expires_in || 10).toString());
  if (timestamp < expiresTimestamp) {
    return;
  }
  const refresh_token = localStorage.getItem('refresh_token');
  const body = {
    grant_type: 'refresh_token',
    client_id: 'console-client-demo',
    client_secret: '1q2w3e*',
    refresh_token
  };
  const qs = require("qs");
  const result = await request<API.LoginResult>('https://localhost:44301/connect/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(body)
  });
  localStorage.setItem('access_token', result.access_token || '');
  localStorage.setItem('expires_in', ((result.expires_in || 10) + moment().unix()).toString());
  localStorage.setItem('refresh_token', result.refresh_token || '');
}

/** 登录接口 POST /api/login/account */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  body.grant_type = 'password';
  body.client_id = 'console-client-demo';
  body.client_secret = '1q2w3e*';
  body.RequireHttpsMetadata = true;
  delete body.autoLogin;
  delete body.type;
  const qs = require("qs");
  return request<API.LoginResult>('https://localhost:44301/connect/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(body),
    ...(options || {}),
  });
}

/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
  return request<API.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 获取规则列表 GET /api/rule */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<API.RuleList>('/api/rule', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 新建规则 PUT /api/rule */
export async function updateRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'PUT',
    ...(options || {}),
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/rule', {
    method: 'DELETE',
    ...(options || {}),
  });
}
